package com.itboye.lottery.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.bean.HistoryPrizeBean;
import com.itboye.lottery.utils.OnItemClickListener;
import com.itboye.lottery.view.FlowLayout;

import java.util.ArrayList;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class HistoryPrizeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    public ArrayList<HistoryPrizeBean.HistoryItem> mData = new ArrayList<>();
    private LayoutInflater inflater;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public HistoryPrizeAdapter(Activity context, ArrayList<HistoryPrizeBean.HistoryItem> mData) {
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_history_prize, parent, false);
        HistoryPrizeViewHolder holder = new HistoryPrizeViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HistoryPrizeViewHolder viewHolder = (HistoryPrizeViewHolder) holder;
        if (mData != null) {
            HistoryPrizeBean.HistoryItem historyItem = mData.get(position);
            viewHolder.item_history_prize_num.setText("第" + historyItem.getNumber() + "期");
            viewHolder.item_history_prize_date.setText(historyItem.getOpen_time());
            ((HistoryPrizeViewHolder) holder).item_history_prize_number.removeAllViews();
            if (historyItem.getRed_num() != null) {
                for (int i = 0; i < historyItem.getRed_num().size(); i++) {
                    ((HistoryPrizeViewHolder) holder).item_history_prize_number.addView(createNumBall(historyItem.getRed_num().get(i), true));
                }
            }
            if (historyItem.getBlue_num() != null) {
                for (int i = 0; i < historyItem.getBlue_num().size(); i++) {
                    ((HistoryPrizeViewHolder) holder).item_history_prize_number.addView(createNumBall(historyItem.getRed_num().get(i), false));
                }
            }
        }
    }


    private TextView createNumBall(String text, boolean isRed) {
        TextView textView = new TextView(context);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(context.getResources().getColor(R.color.white));
        textView.setBackgroundDrawable(context.getResources().getDrawable(isRed ? R.drawable.solid_circle_border_red : R.drawable.solid_circle_border_blue));
//                textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.hollow_circle_border_red));
//                textView.setTextColor(context.getResources().getColor(R.color.main_color));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(4, 4, 0, 4);
        textView.setLayoutParams(layoutParams);
        return textView;
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class HistoryPrizeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView item_history_prize_num, item_history_prize_date;
        FlowLayout item_history_prize_number;
        View view;

        public HistoryPrizeViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            item_history_prize_number = itemView.findViewById(R.id.item_history_prize_number);
            item_history_prize_date = itemView.findViewById(R.id.item_history_prize_date);
            item_history_prize_num = itemView.findViewById(R.id.item_history_prize_num);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }
    }

}
